package org.jeecg.modules.workflow.controller.oa;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.workflow.vo.oa.BpmOALeaveCreateReqVO;
import org.jeecg.common.workflow.vo.oa.BpmOALeavePageReqVO;
import org.jeecg.common.workflow.vo.oa.BpmOALeaveRespVO;
import org.jeecg.common.workflow.vo.oa.BpmOALeaveUpdateReqVO;
import org.jeecg.modules.workflow.controller.BaseController;
import org.jeecg.modules.workflow.convert.oa.BpmOALeaveConvert;
import org.jeecg.modules.workflow.entity.BpmOaLeave;
import org.jeecg.modules.workflow.service.oa.BpmOALeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * OA 请假申请 Controller，用于演示自己存储数据，接入工作流的例子
 *
 * @author jason
 * @author 芋道源码
 */
@Api(tags = "管理后台 - OA 请假申请")
@RestController
@RequestMapping("/bpm/oa/leave")
@Validated
public class BpmOALeaveController extends BaseController {

    @Resource
    private BpmOALeaveService leaveService;

    @Autowired
    private BpmOALeaveConvert bpmOALeaveConvert;

    @PostMapping("/create")
    //@RequiresPermissions("bpm:oa-leave:create")
    @ApiOperation("创建请求申请")
    public Result<Long> createLeave(@Valid @RequestBody BpmOALeaveCreateReqVO createReqVO) {
        return Result.OK(leaveService.createLeave(getUserId(), createReqVO));
    }

    @GetMapping("/get")
    //@RequiresPermissions("bpm:oa-leave:query")
    @ApiOperation("获得请假申请")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public Result<BpmOALeaveRespVO> getLeave(@RequestParam("id") String id) {
        BpmOaLeave leave = leaveService.getLeave(id);
        return Result.OK(bpmOALeaveConvert.convert(leave));
    }

    @GetMapping("/page")
    //@RequiresPermissions("bpm:oa-leave:query")
    @ApiOperation("获得请假申请分页")
    public Result<IPage<BpmOALeaveRespVO>> getLeavePage(@Valid BpmOALeavePageReqVO pageVO) {
        IPage<BpmOaLeave> Page = leaveService.getLeavePage(getUserId(), pageVO);
        IPage<BpmOALeaveRespVO> pageResult = bpmOALeaveConvert.convertPage(Page);
        return Result.OK(pageResult);
    }

    private String getUserId()
    {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return sysUser.getId();
    }

    @PutMapping("/update")
    //@PreAuthorize("@ss.hasPermi('bpm:oa-leave:update')")
    @ApiOperation("更新请假申请")
    public Result<Boolean> updateLeave(@Valid @RequestBody BpmOALeaveUpdateReqVO updateReqVO) {
        leaveService.updateLeave(getUserId(), updateReqVO);
        return Result.OK(true);
    }
}
